package wellness.activities;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import wellness.activities.beans.AgendamentoBean;
import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.TempoLivreBean;
import wellness.activities.util.Dialogs;
import android.R.style;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

public class Schedules extends Activity {

	private Context context;
	View agendamento;
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		popularAgendamentos();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schedules);
		context = this;

		ImageView goBack = (ImageView) findViewById(R.id.imageViewGoBack);
		goBack.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				finish();
			}
		});

		ImageView goHome = (ImageView) findViewById(R.id.imageViewGoHome);
		goHome.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent(Schedules.this, Main.class);
				startActivity(intent);
			}
		});
		
		ImageView newSchedules = (ImageView) findViewById(R.id.imageViewNewSchedule);
		newSchedules.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(), NewSchedule.class);
                startActivityForResult(myIntent, 0);
			}
		});

		popularAgendamentos();

	}

	public void popularAgendamentos() {
		LinearLayout base = (LinearLayout) findViewById(R.id.linearLayoutBaseContent);
		base.removeAllViews();

		GregorianCalendar dataAtual = new GregorianCalendar();

		for (AgendamentoBean a : LoadBeans.getAgendamentos()) {

			if (a.isPraticou() == null) {

				FrameLayout base2 = new FrameLayout(context);
				LinearLayout base3 = new LinearLayout(context);
				base3.setOrientation(LinearLayout.VERTICAL);
				base3.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, Gravity.CENTER_VERTICAL));

				base2.setBackgroundResource(R.drawable.base_pequena);
				base2.setPadding(10, 10, 40, 10);

				TextView atividade = new TextView(context);
				atividade.setText(a.getAtividade().getNome());
				atividade.setPadding(20, 0, 0, 0);
				atividade.setTextAppearance(this, style.TextAppearance_Medium);
				atividade.setTextColor(Color.WHITE);
				atividade.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));

				TextView horario = new TextView(context);
				SimpleDateFormat br = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
				horario.setText(br.format(a.getInicio().getTime()));
				horario.setPadding(20, 0, 0, 0);
				horario.setTextAppearance(this, style.TextAppearance_Small);

				horario.setTextColor(Color.WHITE);
				horario.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));

				if (a.getInicio().after(dataAtual)) {

					ImageView remove = new ImageView(context);
					remove.setBackgroundResource(R.drawable.remover);
					remove.setId(a.getId());
					remove.setLayoutParams(new LayoutParams(30, 30,
							Gravity.CENTER_VERTICAL | Gravity.RIGHT));
					remove.setOnClickListener(new View.OnClickListener() {

						public void onClick(View view) {
							removerAgendamento(view);
						}

					});

					base2.addView(remove);
				} else {
					ImageView pendente = new ImageView(context);
					pendente.setBackgroundResource(R.drawable.icon_attention);
					pendente.setLayoutParams(new LayoutParams(30, 30,
							Gravity.CENTER_VERTICAL | Gravity.RIGHT));
					base2.setId(a.getId());
					base2.setOnClickListener(new View.OnClickListener() {

						public void onClick(View view) {
							Agenda.visualizarPendencia(view);
						}

					});
					
					base2.addView(pendente);
				}

				base3.addView(atividade);
				base3.addView(horario);
				base2.addView(base3);
				base.addView(base2);

			}
		}
	}

	public void removerAgendamento(View v) {
		agendamento = v;

		DialogInterface.OnClickListener sim = new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int arg1) {
				LoadBeans.removerTempoLivrePorId(agendamento.getId());
				FrameLayout base2 = (FrameLayout) agendamento.getParent();
				LinearLayout base1 = (LinearLayout) base2.getParent();
				base1.removeView(base2);
				agendamento = null;
			}
		};

		Dialogs.showDialog("Aviso",
				"Deseja reamente remover esse agendamento?", sim, null, context);

	}

}
